<?php
require_once dirname(__FILE__) . "/../config/functions.php";
include_once dirname(__FILE__) . '/header.php';

$sendFlag = FALSE;
if (isset($_POST) && $_POST) {
    if ($_POST['email_subject'] && $_POST['email_body']) {
        $users = mysql_query("SELECT * from user where status <> 2");
        while ($row = mysql_fetch_array($users)) {
            $search = array('{name}', '{username}', '{email}', '{password}');
            $replace = array($row['name'], $row['username'], $row['email'], $row['password']);
            $subject = str_replace($search, $replace, $_POST['email_subject']);
            $body = str_replace($search, $replace, $_POST['email_body']);
            $to = $row['email'];
            if (sendMail($to, $subject, $body)) {
                $sendFlag = TRUE;
            }
        }
    }
}
?>
<div id="page-wrapper">

    <div class="row">
        <div class="col-lg-12">
            <h1>Edit <small>Page</small></h1>
            <?php if ($sendFlag) : ?>
                <div class="alert alert-success alert-dismissable">
                    <button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button>
                    Email sent successfully.
                </div>
            <?php endif; ?>

            <form class="form-horizontal" role="form" method="post">

                <p class="field-note">You can use the following tags for personalized mailing and these will automatically get replaced by user information.</p>
                <p class="field-note">{name} for Name of the member</p>
                <p class="field-note">{username} for username of the member</p>
                <p class="field-note">{password} for the password of the member</p><p class="field-note">
                <p class="field-note">{email} for the Email Address of the member</p>

                <div class="form-group">
                    <label for="email_subject" class="col-sm-2 control-label">Subject</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" name="email_subject" id="email_subject">
                    </div>
                </div>

                <div class="form-group">
                    <label for="email_body" class="col-sm-2 control-label">Body</label>
                    <div class="col-sm-10">
                        <textarea class="form-control" rows="10" name="email_body" id="email_body"></textarea>
                    </div>
                </div>

                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-primary">Send</button>
                    </div>
                </div>
            </form>

        </div>
    </div><!-- /.row -->

</div><!-- /#page-wrapper -->
<?php include_once dirname(__FILE__) . '/footer.php'; ?>

